#include "shaderloader.h"
#include "library.h"
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

string textFileRead(string file) {
	ifstream is(file.c_str());
	ostringstream os;

	while (is.good()) {
		string line;
		getline(is, line);
		os << line << "\n";
	}

	is.close();

	return os.str();
}

GLuint createShaderProgram(string vert, string frag) {
	char log[1024] ;
	int len;


	GLuint v = glCreateShader(GL_VERTEX_SHADER);
	GLuint f = glCreateShader(GL_FRAGMENT_SHADER);

	string vs = textFileRead(vert);
	string fs = textFileRead(frag);

	const char* vcstr = vs.c_str();
	const char* fcstr = fs.c_str();

	glShaderSource(v, 1, &vcstr, NULL);
	glShaderSource(f, 1, &fcstr, NULL);

	glCompileShader(v);
	glGetShaderInfoLog(v, 1000, &len, log);
	cout<<"V: "<<log<<endl;

	
	glCompileShader(f);
	glGetShaderInfoLog(f, 1000, &len, log);
	cout<<"F: "<<log<<endl;
	

	GLuint p = glCreateProgram();

	glAttachShader(p,v);
	glAttachShader(p,f);

	glLinkProgram(p);

	glGetProgramInfoLog(p, 1000, &len, log);
	cout<<"P: "<<log<<endl;
	return p;
}
